/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.sendmsg.service.template  
 */
package com.dwusoft.sendmsg.service.template;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.database.constant.SpringContextLoader;
import com.apache.database.constant.Validator;
import com.apache.api.vo.ParamsVo;
import com.dwusoft.sendmsg.common.entity.Sendmsg;
import com.dwusoft.sendmsg.manager.SendmsgManager;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;

/**
 * description:  
 * @author Hou Dayu 创建时间：2016-5-18  
 * @Copyright(c)2014:北京东吴软件技术有限公司
 */
public class SendmsgTask implements Runnable {

	private SendmsgManager sendmsgManager;
	private transient static boolean SendmsgTaskMark = true;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see java.lang.Runnable#run()  
	 */
	public void run() {
		int count = Calendar.getInstance().getTime().getHours();
		if (6 <= count && count <= 24) {
			try {
				if (SendmsgTaskMark) {
					SendmsgTaskMark = false;
					sendmsgManager = (SendmsgManager) SpringContextLoader.getBean("sendmsgManager");
					ParamsVo<Sendmsg> vo = new ParamsVo<Sendmsg>();
					Sendmsg msg = new Sendmsg();
					msg.setMsgStatus("1");
					vo.setObj(msg);
					List<Sendmsg> list = sendmsgManager.getList(vo);
					if (Validator.isEmpty(list))
						return;
					for (Sendmsg smsg : list) {
						long num = 0;
						if ("email".equalsIgnoreCase(smsg.getMsgType())) {
							num = sendEmail(smsg);
						} else if ("mobile".equalsIgnoreCase(smsg.getMsgType())) {
							num = sendMobile(smsg);
						} else if ("all".equalsIgnoreCase(smsg.getMsgType())) {
							num = sendEmail(smsg);
							if (num < 1) {
								num = sendMobile(smsg);
							}
						} else {
							continue;
						}
						if (num > 0) {
							long passNum = Integer.parseInt(StrUtil.doNull(smsg.getMsgPassNum(), "0"));
							smsg.setMsgPassNum(String.valueOf(passNum + num));
							if (passNum + num >= 100) {
								smsg.setMsgStatus("2");
								smsg.setMsgSendTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
							}
						} else {
							smsg.setMsgStatus("1");
							smsg.setMsgSendTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
						}
						vo.setObj(smsg);
						sendmsgManager.editInfo(vo);
					}
				}
			} catch (Exception e) {
			} finally {
				SendmsgTaskMark = true;
			}
		}
	}

	private long sendMobile(Sendmsg sendMsg) {
		long mark = 0;
		String mobile = sendMsg.getToUserMobile();
		if (Validator.isNull(mobile))
			return 1;
		Map<String, String> params = new HashMap<String, String>();
		boolean flag = TemplateHelper.instance().getSends("mobile").sendMessage(sendMsg, params);
		if (!flag)
			return 1;
		return mark;
	}

	private long sendEmail(Sendmsg sendMsg) {
		long mark = 0;
		Map<String, String> params = new HashMap<String, String>();
		String email = sendMsg.getToUserEmail();
		if (Validator.isNull(email))
			return 1;
		params.put("tempName", StrUtil.doNull(sendMsg.getSpare1(), "sendEmail.ftl"));
		boolean flag = TemplateHelper.instance().getSends("email").sendMessage(sendMsg, params);
		if (!flag)
			return 1;
		return mark;
	}
}
